package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceimpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    @Log
    public void addClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazz.setCreateTime(LocalDateTime.now());
        clazzMapper.addClazz(clazz);
    }

    @Override
    public PageBean selectClazzPage(ClazzQueryParam clazzQueryParam) {
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        List<Clazz> list = clazzMapper.selectPage(clazzQueryParam);
        Page<Clazz> p = (Page<Clazz>) list;
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public List<Clazz> selectClazzAll() {
        return clazzMapper.selectClazzAll();
    }

    @Override
    @Log
    public void deleteClazzById(Integer id) {
        Integer clazzId = id;
        List<Student> list = studentMapper.selectByclazzId(clazzId);
        if(list.size()>0){
            throw new BusinessException("班级中还有学生不能删除");
        }
        clazzMapper.deleteClazzById(id);
    }

    @Override
    public Clazz selectClazzByid(Integer id) {
        return clazzMapper.selectClazzById(id);
    }

    @Override
    @Log
    public void updateClazz(Clazz clazz) {

        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);
    }

//    @Override
//    public List<Clazz> selectAll() {
//        return clazzMapper.selectAll();
//    }
}
